/*
 * !Drowning
 * May 29, 2011
 * TCSS 360
 * Schedule Project
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * The Courses will provide Collection of courses
 *  
 * @author Efim Todorasco
 * @author David Phillips modified and reviewed
 * @version may 27, 2011
 *
 */
public class Courses{
     
     private List<Course> my_courses = new ArrayList<Course>();
     
     public Courses(String the_schedule_csv) {
    	 courseParser(the_schedule_csv);
     }
     
     public List<Course> getCourses(){
    	 return my_courses;
     }
     
 	 /**
 	  * Parses the courses from the schedule.
 	  *  
 	  * @param the_schedule_csv The schedule.
 	  */
     private void courseParser(String the_schedule_csv) {
    	BufferedReader reader;
    	List<String> course_information = new ArrayList<String>();
 		String parsedLine;
		StringTokenizer tokenizer;
		String token;
		int counter = 0;
		Map the_map = new HashMap();
 		try
 		{	
 			reader = new BufferedReader( new FileReader(the_schedule_csv));
 			while((parsedLine = reader.readLine()) != null)
 			{
 				if (parsedLine.startsWith("T")) 
 				{
 					tokenizer = new StringTokenizer(parsedLine, ",");
 				
 					while(tokenizer.hasMoreTokens())
 					{	
 						token = tokenizer.nextToken();
 						if (token.equals("TBA")) token = "-1";
 						course_information.add(token);
 						
 					}
 					if(the_map.containsKey(course_information.get(0))){
 						my_courses.get(
 								(Integer) the_map.get(
 										course_information.get(0))).add(course_information);
 					}
 					else{
 						my_courses.add(new Course(course_information));
 						the_map.put(course_information.get(0), counter);
 						counter++;
 					}
 					course_information.clear();
 				}
 			}
  
 		}
 		catch(Exception e)
 		{
 			System.err.print(e);
 		}
 	} 
}